//
//  XMFeedbackView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/22.
//

#import "XMFeedbackView.h"
#import "XMFeedbackFootView.h"

@interface XMFeedbackView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tbl;
@property (nonatomic, strong) XMFeedbackFootView *feedbackFootView;

@property (nonatomic, strong) NSArray *titleArr;

@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation XMFeedbackView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"意见反馈";
    
    _titleArr = @[@"功能异常：功能故障或者不可用",
                  @"产品建议：功能用的不爽想建议",
                  @"安全问题：密码、数据、隐私等",
                  @"其他问题"];
    [self addSubview:self.tbl];
}

#pragma mark - Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return KScaleWidth(56.0);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.width, KScaleWidth(56.0))];
    headView.backgroundColor = kColorHEX(0xffffff);
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(20.0), KScaleWidth(12.0), headView.width - KScaleWidth(20.0), KScaleWidth(32.0)) font:KBoldFontSize(15.0) textColor:kColorHEX(0x333333)];
    titleLabel.text = @"请选择您想反馈的问题点（必选）";
    [headView addSubview:titleLabel];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(45.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellName = @"XMFeedbackCellName";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        cell.textLabel.font = KFontSize(16.0);
        cell.textLabel.textColor = kColorHEX(0x333333);
        
        UIImageView *selectImgView = [[UIImageView alloc] initWithFrame:CGRectMake(tableView.width - KScaleWidth(20.0) - KScaleWidth(24.0), (KScaleWidth(45.0) - KScaleWidth(24.0))/2, KScaleWidth(24.0), KScaleWidth(24.0))];
        [selectImgView setCornerRadius:selectImgView.height/2];
        selectImgView.tag = 1032;
        selectImgView.contentMode = UIViewContentModeScaleAspectFit;
        [cell addSubview:selectImgView];
    }
    UIImageView *selectImg = [cell viewWithTag:1032];
    if (!selectImg) {
        UIImageView *selectImgView = [[UIImageView alloc] initWithFrame:CGRectMake(tableView.width - KScaleWidth(20.0) - KScaleWidth(24.0), (KScaleWidth(45.0) - KScaleWidth(24.0))/2, KScaleWidth(24.0), KScaleWidth(24.0))];
        [selectImgView setCornerRadius:selectImgView.height/2];
        selectImgView.tag = 1032;
        selectImgView.contentMode = UIViewContentModeScaleAspectFit;
        [cell addSubview:selectImg];
    }
    if (_selectIndex == indexPath.row) {
        selectImg.image = nil;
        selectImg.backgroundColor = kColorHEX(0xFFD800);
    }
    else {
        selectImg.image = ImageNamed(@"");
        selectImg.backgroundColor = kColorHEX(0x999999);
    }
    cell.textLabel.text = _titleArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _selectIndex = indexPath.row;
    [tableView reloadData];
}

#pragma mark - Init
- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH) style:UITableViewStylePlain];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        _tbl.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tbl.tableFooterView = self.feedbackFootView;
//        [_tbl registerClass:<#(nullable Class)#> forCellReuseIdentifier:<#(nonnull NSString *)#>];
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
    }
    return _tbl;
}

- (XMFeedbackFootView *)feedbackFootView {
    if (!_feedbackFootView) {
        _feedbackFootView = [[XMFeedbackFootView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(570.0))];
        XMWeakSelf;
        _feedbackFootView.submitBlock = ^(NSString * _Nonnull content, NSArray * _Nonnull selectImgArr) {
            weakSelf.submitBlock ? weakSelf.submitBlock(weakSelf.titleArr[weakSelf.selectIndex], content, selectImgArr) : nil;
        };
    }
    return _feedbackFootView;
}

//- (UIScrollView *)bodyScrollView {
//    if (!_bodyScrollView) {
//        _bodyScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH)];
//    }
//    return _bodyScrollView;
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
